package com.recruit.g1.user.service.impl;

import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.common.response.StateCode;
import com.recruit.g1.pojo.passport.param.UserLoginParam;
import com.recruit.g1.pojo.user.dto.UserRegParam;
import com.recruit.g1.pojo.user.entity.User;
import com.recruit.g1.pojo.user.vo.UserSaveParam;
import com.recruit.g1.user.dao.mapper.UserMapper;
import com.recruit.g1.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {

//    @Autowired
//    private UserService userService;

    @Override
    public JsonResult register(UserRegParam userRegParam) {
        UserSaveParam userSaveParam = userMapper.selectByUsername(userRegParam.getUsername());

        log.debug("完成selectByUsername");
        if (userSaveParam!=null){
            return JsonResult.fail(StateCode.USERNAME_EXISTS);
        }

        userMapper.insert(userRegParam);
        log.debug("注册数据："+userRegParam);
        return JsonResult.ok("注册成功！");
    }


    private static final Logger log = LogManager.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;

    @Override
    public JsonResult login(UserLoginParam userLoginParam, @ApiIgnore HttpSession httpSession) {
        User user =
           userMapper.selectByUsernameAndPassword(userLoginParam.getUsername(),userLoginParam.getPassword());
        user.setPassword("");
        System.out.println("UserServiceImpl::login:"+user);
        if (user==null){
            System.err.println("用户名或密码错误");
            return JsonResult.fail(StateCode.USERNAME_PASSWORD_ERROR);
        }
        httpSession.setMaxInactiveInterval(10*60*1000);
        httpSession.setAttribute("currentLoginUser",user);//////////////////////////////////////
//        System.out.println("currentLoginUser-login()::"+httpSession.getAttribute("currentLoginUser"));
//        System.out.println(httpSession);
//        System.out.println(httpSession.getId());
        return JsonResult.ok(user);
        //return JsonResult.ok("登录成功！");
    }


    /**
     * 重置密码(修改密码)
     * @param newPassword
     * @param httpSession
     * @return
     */
    @Override
    public JsonResult resetPassword(String newPassword,@ApiIgnore HttpSession httpSession) {
        UserSaveParam userSaveParam = (UserSaveParam) httpSession.getAttribute("currentLoginUser");
        if (userSaveParam==null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }
        int result =
        userMapper.updatePassword(userSaveParam.getUsername(), userSaveParam.getPassword() ,newPassword);

        if (result==1){
            return JsonResult.ok("修改密码成功");
        }

        return JsonResult.fail("密码修改失败");
    }
}
